/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Rick
 */
class mySQLVisualizationDAO implements VisualizationDAO {
    PreparedStatement ps = null;
    ResultSet rs = null;
    Connection conn = null;

    public mySQLVisualizationDAO() {
        conn = SQLConfigeration.getConnection();
    }

    /**
     * Used to insert into the Visualization table
     *
     * @param visualName
     * @return boolean insertSuccess
     */
    public boolean insertVisualization(String visualName) {
        boolean insertSuccess = false;
        String insertVisual = "insert into Visualization(name) values (?)";
        try{
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(insertVisual);
            ps.setString(1, visualName);
            System.out.println("Attempting insert");
            ps.executeUpdate();
            conn.commit();
            System.out.println("Insert Successfull");
            insertSuccess = true;
        }catch(SQLException ex){
            System.out.println("Visualization already exists\nVisualization Insert Failed");
        }finally{
            try {
                ps.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return insertSuccess;
    }

    /**
     * Currently not implemented
     * @param visualName
     * @return boolean deleteSuccess
     */
    public boolean deleteVisualization(String visualName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean findVisualization(String visualName) {
        boolean found = false;
        String query = "Select name from Visualization where name = ?";
        try{
            ps = conn.prepareStatement(query);
            ps.setString(1, visualName);
            rs = ps.executeQuery();

            rs.next();

            System.out.println("Attempting to see if Visualization exists");
            rs.getString("name");
            found = true;
            System.out.println("Visualization found");

        }catch(SQLException ex){
            found = false;
            System.out.println("Visualization not Found");
            //System.out.println("Visualization Error " + ex);
        }finally{
            try {
                rs.close();
                ps.close();
                //conn.close();
            } catch (SQLException ex) {
                System.out.println("SQL Error: " + ex);
            }
        }
        return found;
    }

    /**
     * Currently not implemented
     * @param visualName
     * @return boolean updateSuccess
     */
    public boolean updateVisualization(String visualName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
